package testJdk.commonUse.number.testDecimalConversion;

import org.junit.Test;

/**
 * 进制转换
 */
public class TestConversion {

    /**
     * 将其他进制转换成10进制
     */
    @Test
    public void test01() {
        String binaryString = "11010001";
        int result01 = Integer.parseInt(binaryString, 2);
        System.out.println(result01);

        String octalString = "321";
        int result02 = Integer.parseInt(octalString, 8);
        System.out.println(result02);

        String hexString = "d1";
        int result03 = Integer.parseInt(hexString, 16);
        System.out.println(result03);
    }

    /**
     * 将10进制,转换成16，8，2进制转换成
     */
    @Test
    public void test02() {
        int num = 209;

        // 10进制 --> 16进制
        String hexString = Integer.toHexString(num);
        System.out.println(hexString);

        // 10进制 --> 8进制
        String octalString = Integer.toOctalString(num);
        System.out.println(octalString);

        // 10进制 --> 2进制
        String binaryString = Integer.toBinaryString(num);
        System.out.println(binaryString);
    }
}
